﻿Public Class BuscCliente

    Dim classController As New Class_RC_Controller.rc_Clientes()

    Private m_frmPedido As FrmPedidosN
    Public Property frmPedidos() As FrmPedidosN
        Get
            Return m_frmPedido
        End Get
        Set(value As FrmPedidosN)
            m_frmPedido = value
        End Set
    End Property

    Private _codCliente As Integer = 0

    Public Property codCliente() As Integer
        Get
            Return _codCliente
        End Get
        Set(value As Integer)
            _codCliente = value
        End Set
    End Property

    Private _rucCliente As String = 0

    Public Property rucCliente() As String
        Get
            Return _rucCliente
        End Get
        Set(value As String)
            _rucCliente = value
        End Set
    End Property

    Private _nombreCliente As String = 0

    Public Property nombreCliente() As String
        Get
            Return _nombreCliente
        End Get
        Set(value As String)
            _nombreCliente = value
        End Set
    End Property

    Private _telfCliente As String = 0

    Public Property telefCliente() As String
        Get
            Return _telfCliente
        End Get
        Set(value As String)
            _telfCliente = value
        End Set
    End Property

    Private _direccCliente As String = 0

    Public Property direccCliente() As String
        Get
            Return _direccCliente
        End Get
        Set(value As String)
            _direccCliente = value
        End Set
    End Property

    Private Sub BuscCliente_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgClientes.AutoGenerateColumns = False
        CargarCombos()
        CargarClientes()
    End Sub

    Private Sub CargarClientes()

        Dim codTipoCliente As Integer
        If cbxTipoCliente.Checked Then
            codTipoCliente = cboTipoCliente.SelectedValue
        End If

        Dim codCiudad As Integer
        If cbxCiudad.Checked Then
            codCiudad = cboCiudad.SelectedValue
        End If

        Dim dt As New DataTable
        dt = classController.get_Clientes_All(oiml.dbConn, 0, txtNombre.Text, txtRuc.Text, codTipoCliente, codCiudad)
        dgClientes.DataSource = dt

    End Sub

    Private Sub CargarCombos()
        Dim dsTipo As New DataTable
        dsTipo = classController.get_tipo_Clientes(oiml.dbConn)
        If Not dsTipo Is Nothing Then
            If dsTipo.Rows.Count > 0 Then
                cboTipoCliente.DataSource = dsTipo
                cboTipoCliente.DisplayMember = "descripcion"
                cboTipoCliente.ValueMember = "codtipocliente"
            End If
        End If

        Dim dsCiudad As New DataTable
        dsCiudad = classController.get_Ciudades(oiml.dbConn)
        If Not dsCiudad Is Nothing Then
            If dsCiudad.Rows.Count > 0 Then
                cboCiudad.DataSource = dsCiudad
                cboCiudad.DisplayMember = "descripcion"
                cboCiudad.ValueMember = "codCiudad"
            End If
        End If
    End Sub

    'Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
    '    Me.Close()

    'End Sub

    'Private Sub btnEditar_Click(sender As System.Object, e As System.EventArgs) Handles btnEditar.Click
    '    Try
    '        If dgClientes.SelectedRows.Count > 0 Then
    '            Dim cod As New Integer
    '            cod = dgClientes.SelectedRows(0).Cells(0).Value

    '            If frmClientes Is Nothing Then

    '                frmClientes = New FrmClientes()
    '                frmClientes.codCliente = cod

    '                'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
    '                frmClientes.ShowDialog()
    '                CargarClientes()
    '                frmClientes.Dispose()
    '                frmClientes = Nothing
    '                'End If
    '            Else
    '                frmClientes.codCliente = cod
    '                frmClientes.ShowDialog()
    '            End If

    '        End If

    '    Catch ex As Exception
    '        MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

    '    End Try

    'End Sub

    Private Sub txtBuscar_Click(sender As System.Object, e As System.EventArgs) Handles txtBuscar.Click
        CargarClientes()
    End Sub

    Private Sub txtNombre_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNombre.KeyDown, txtRuc.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarClientes()

        End If
    End Sub

    Private Sub dgClientes_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgClientes.DoubleClick
        Seleccionar()
    End Sub

    Private Sub dgClientes_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dgClientes.KeyDown
        If e.KeyCode = Keys.Enter Then
            Seleccionar()
        End If

    End Sub

    Private Sub cbxTipoCliente_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxTipoCliente.KeyDown, cbxCiudad.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarClientes()
        End If
    End Sub

    Private Sub cboTipoCliente_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboTipoCliente.KeyDown, cboCiudad.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarClientes()
        End If
    End Sub

    Private Sub Seleccionar()
        Try
            If dgClientes.SelectedRows.Count > 0 Then

                codCliente = dgClientes.SelectedRows(0).Cells(0).Value
                nombreCliente = dgClientes.SelectedRows(0).Cells(2).Value.ToString
                rucCliente = dgClientes.SelectedRows(0).Cells(3).Value.ToString
                telefCliente = dgClientes.SelectedRows(0).Cells(4).Value.ToString
                direccCliente = dgClientes.SelectedRows(0).Cells(6).Value.ToString

                Me.Close()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try
    End Sub

    Private m_frmCliente As FrmClientes
    Public Property frmClientes() As FrmClientes
        Get
            Return m_frmCliente
        End Get
        Set(value As FrmClientes)
            m_frmCliente = value
        End Set
    End Property

    Private Sub btnNuevo_Click(sender As System.Object, e As System.EventArgs) Handles btnNuevo.Click
        frmClientes = New FrmClientes
        frmClientes.ShowDialog()
        CargarClientes()
    End Sub

End Class